<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Services;

use App\Models\Common\Invitation;
use Illuminate\Http\Request;

class InvitationService
{
    use Service;

    /**
     * 详情
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function InvitationTreeService( $request )
    {
        if ( ! $request->filled('pid') ) request()->offsetSet('pid', 0);
        $where      = function ( $query ) use ( $request ) {
            if ( $request->filled('name') ) $query->whereHas('pUser', function ( $query ) use ( $request ) {
                $query->where('name', $request->input('name'))->orWhere('phone', $request->input('name'))->orWhere('email', $request->input('name'));
            });
            else $query->where('p_user_id', $request->input('pid'));
        };
        $with       = [
            'user' => function ( $query ) {
                $query->select('id', 'name', 'email');
            },
        ];
        $select     = [ 'id', 'user_id', 'p_user_id' ];
        $invitation = Invitation::GetModel($where, $with, $select);
        foreach ( $invitation as &$item ) {
            $item->name     = $item->user->name . ' (' . $item->user->email . ' | ' . $item->user->id . ' )';
            $item->children = $this->TreeChild($item->user_id, $with, $select);
        }
        return data($invitation);
    }

    /**
     *树列表
     *
     * @param $user_id
     * @param $with
     * @param $select
     *
     * @return Invitation[]|\Illuminate\Database\Eloquent\Collection
     */
    protected function TreeChild( $user_id, $with, $select )
    {
        $invitation = Invitation::GetModel([ 'p_user_id' => $user_id ], $with, $select);
        foreach ( $invitation as &$item ) {
            $item->name     = $item->user->name . ' (' . $item->user->email . ' | ' . $item->user->id . ' )';
            $children       = $this->TreeChild($item->user_id, $with, $select);
            $item->children = $children ? : [];
        }
        return $invitation;
    }

    /**
     * 团队人数
     *
     * @param      $user_id
     * @param      $count
     * @param bool $i
     *
     * @return int
     */
    public function TreeChildCount( $user_id, $count, $i = TRUE )
    {
        $select     = [ 'id', 'user_id', 'p_user_id' ];
        $invitation = Invitation::GetModel([ 'p_user_id' => $user_id ], [], $select);
        $count      = count($invitation) + $count;
        if ( $i ) return $count;
        foreach ( $invitation as $item ) $count = $this->TreeChildCount($item->user_id, $count, $i);
        return $count;
    }
}
